unit UFiltraCadastro;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, DBCtrls, DB, DBTables, DBLists;

type
  TFFiltraCadastro = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    BtnFiltrar: TBitBtn;
    BtnCancelar: TBitBtn;
    Panel3: TPanel;
    Panel4: TPanel;
    RGCondicao: TRadioGroup;
    RGOperador: TRadioGroup;
    Panel5: TPanel;
    MFiltra: TMemo;
    LBCampos: TListBox;
    procedure BtnFiltrarClick(Sender: TObject);
    procedure BtnCancelarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure LBCamposKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FFiltraCadastro: TFFiltraCadastro;
implementation

uses UFomCadastro;

{$R *.dfm}




procedure TFFiltraCadastro.BtnFiltrarClick(Sender: TObject);
begin
 FormCadastro.Filtra(true);
end;

procedure TFFiltraCadastro.BtnCancelarClick(Sender: TObject);
begin
 FormCadastro.Filtra(False);
end;

procedure TFFiltraCadastro.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
 Self.Release;
end;

procedure TFFiltraCadastro.LBCamposKeyPress(Sender: TObject;
  var Key: Char);

Var
 p     		:Integer;
 op,F,Filtro    :String;
begin
 if UpperCase(key) = 'I' then
  begin
   if Length(MFiltra.Lines.Text) = 0 then
    op:=' '
   else
    begin
     if RGOperador.ItemIndex = 0 then // And
      Op:=' And '
     else
      if RGOperador.ItemIndex = 1 then // OR
       op:=' Or '
      else
       if RGOperador.ItemIndex = 2 then // Nada
        op:=' ';
    end;

   case RGCondicao.ItemIndex of
    0 : begin // =
         F:=InputBox('Filtro para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         Filtro:= op + ' (' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' = ' + QuotedStr(f) + ')';
        end;

    1 : begin // >
         F:=InputBox('Filtro para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         Filtro:= op + ' (' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' = ' + QuotedStr(f) + ')';
        end;

    2 : begin // >=
         F:=InputBox('Filtro para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         Filtro:= op + ' (' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' = ' + QuotedStr(f) + ')';
        end;

    3 : begin // <
         F:=InputBox('Filtro para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         Filtro:= op + ' (' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' = ' + QuotedStr(f) + ')';
        end;

    4 : begin // <=
         F:=InputBox('Filtro para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         Filtro:= op + ' (' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' = ' + QuotedStr(f) + ')';
        end;

    5 : begin // between --> >= and <=
         F:=InputBox('Filtro 1 para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         F:=QuotedStr(f);
         Filtro:= op + ' ( ' + LBCampos.Items.Strings[LBCampos.ItemIndex] ;
         Filtro:= Filtro + ' >=  ' + f ;
         F:=InputBox('Filtro 2 para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         F:=QuotedStr(f);
         Filtro:= Filtro + '  And  ' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' <= ' + f + ')';
        end;

    6 : begin //Iniciando Com  Like
         F:=InputBox('Filtro para :' + LBCampos.Items.Strings[LBCampos.ItemIndex],'','');
         Filtro:= op + ' (' + LBCampos.Items.Strings[LBCampos.ItemIndex]  + ' =  ' + QuotedStr(f + '*') + ')';
        end;

    7 : begin // Is Null
         Filtro:= op + '(' + LBCampos.Items.Strings[LBCampos.ItemIndex] + ' is null )' ;
        end;
   end;//Case
   MFiltra.Lines.Add(Filtro);
  end
 else
  if UpperCase(key) = 'A' then
   begin
    for p:=0 to MFiltra.Lines.Count - 1 do
     begin
      if pos(LBCampos.Items.Strings[LBCampos.ItemIndex],MFiltra.Lines.Strings[p]) <> 0 then
       MFiltra.Lines.Delete(p);
     end;
   end

end;

end.
